Reading and Writing Files
This is a really exciting lecture cause you’re gonna learn how to read data from files, and also how to write data into files.
And we already imported the built-in module that we need for that in the last lecture, and so let’s now get rid of this code and start using the FS module. So we use fs.readfilesync, okay. And sync stands for synchronous, and you will start learning about synchronous and asynchronous right in the next lecture, okay. So this is the synchronous version of file reading. There is also an asynchronous version.
So, the read file sync function takes two arguments. The first one is the path to the file that we’re reading and then also the character encoded.
So, first the path to the file, and the file is in the txt folder, and it is the input one. So, this text that I have here about the avocado is what we’re gonna read into a variable. Okay, so we specified a path there, and there are multiple ways of doing that, but for now I’m gonna go with the simplest one. So, all I’m gonna do is set it in the /txt/input.txt. So again, we’re starting at the home folder, which is basically the folder where the index.js file is located, so that’s the dot in there, and then we move into the txt folder, and from there input.txt. Then here the second one we have to define the character encoding, which is utf8, usually, at least if you’re just using English. Okay, and if you don’t specify this, we get back something called a buffer, okay, and that’s not really what we want. We really just want the text.
And so, calling this function here will now read the data from the file and return it to us, and so we need save that somewhere and we put it into a variable. So let’s say textIn because it’s a more text input. Okay, and then let’s log it to the console just to see if it works. Give it a second, and now we need to run this here again. So in the terminal, I can just use the up arrow to run the previous command. So here it is nodeindex.js again. Hit return, and let’s see, and indeed here we go. So here is the content of that file. So, perfect.
We now know how to read stuff from files. Congratulations. Okay, but we also want to know how to write to files.
So let’s create some new variable here with some more text, and then write it into a new file. So let’s call that one textOut, so for output. And basically let’s just write a string where we will then include this text in. I’m gonna be using a template string here, and at this point I’m gonna assume that you’re kinda familiar with the es6 syntax, okay. So we already used const here, which is an es6 way of declaring variables instead of var, and now the template string, which is another es6 thing. So before the es6, if you wanted to add something to text in, you would have to use the plus operator. So let’s say, “this is” and then space and then text in. Okay, so you would have to use the plus operator, but if it’s a template string it’s much easier. All we have to do is use the backticks, then some text, and then into this string you can very easily plug in the variable. So let’s say, “This is what we know about the avocado.” Then we use this syntax to input the variable, okay, and so basically inside of these curly braces here we can write any JavaScript that we want. So it’s not just for plugging in variables straight away, we can also do a lot of calculations or any javaScript expression that we wanted in here. Now, let’s say a new line character. So that’s a new line, okay. And just to show you that we can actually use JavaScript inside of these curly braces here. So this dollar sign and then curly braces. So date, or actually date like this, dot now. Okay, so that is our string, which has this text here in together with the text that we read before from the variable.
And so now let’s write that to a new file. So again, we use the FS module, and this time, writeFileSync. Okay, and again we specify the path to the file, and we still want it in the txt folder, and we call it output.txt, okay. And now we have to actually specify what we want to write into that file, right, and tell us the text out variable. And this doesn’t return anything meaningful until we don’t save anything to any variable. All we do is to finally log something more to the console, like just informing if the file has been written. Okay, so let’s test it out again.
I’m gonna clear the console with command K. Hit the arrow up key, enter, and maybe you saw it.